package MemberController;

import java.io.IOException;

import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MemverRepAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
    public MemverRepAction() {
        super();
   
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("*****회원정보수정*****");

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		String action_code = request.getParameter("action_code");
		
		RequestDispatcher view = null;
		PrintWriter out =response.getWriter();
		
		MembersBean mbean = new MembersBean();
		MembersDAO mdao = new MembersDAO();
		
		if(action_code.equals("list")){
			String numMode = request.getParameter("num");
			int start = numMode != null ? Integer.parseInt(numMode) : -1;
			System.out.println(start);
			switch(start){
			case 1:
				request.setAttribute("upcheck","1");
				request.setAttribute("check","/Main/FirstMain/Fm_CheckFind.jsp");
				view = request.getRequestDispatcher("/Main/FirstMain/Fm_Update.jsp");
				view.forward(request, response);
			break;
			
			
			case 2:
				boolean check = false;
				mbean.setMem_Nick(request.getParameter("Mem_Nick"));
				mbean.setMem_Pass(request.getParameter("Mem_Pass"));
				System.out.println(request.getParameter(request.getParameter("Mem_Pass")));
				check=mdao.mCheckPass(mbean);
					System.out.println(check);
				if(check==true){
						
						ArrayList memberList = mdao.memberList(mbean);
						request.setAttribute("upcheck","2");
						request.setAttribute("memberList", memberList);
						request.setAttribute("check","/Main/FirstMain/Fm_CheckUpdate.jsp");
						view = request.getRequestDispatcher("/Main/FirstMain/Fm_Update.jsp");
						view.forward(request, response);
						System.out.println("*****비밀번호 체크 성공*****");
					
					}else{
						out.println( "<script>" );
						out.println( "alert(\"PASSが間違いました.\");" );
						out.println( "location.href=\"javascript:history.go(-1)\";" );
						out.println( "</script>" );
					}
				
				break;
			
			case 3:
				
				System.out.println("*****정보수정하기*****");
				
				mbean.setMem_Nick(request.getParameter("Mem_Nick"));
				mbean.setMem_Spost(request.getParameter("Mem_Spost"));
				mbean.setMem_Fpost(request.getParameter("Mem_Fpost"));
				mbean.setMem_Address1(request.getParameter("Mem_Address1"));
				mbean.setMem_Address2(request.getParameter("Mem_Address2"));
				mbean.setMem_Phone(request.getParameter("Mem_Phone"));
				mbean.setMem_Fax(request.getParameter("Mem_Fax"));
				mbean.setMem_Email(request.getParameter("Mem_Email"));
				mbean.setMem_Mobile(request.getParameter("Mem_Mobile"));
				mdao.mUpdate(mbean);
				ArrayList memberList = mdao.memberList(mbean);
				
				request.setAttribute("memberList",memberList);
				request.setAttribute("upcheck","3");
				request.setAttribute("check","/Main/FirstMain/Fm_Conf.jsp");
				view = request.getRequestDispatcher("/Main/FirstMain/Fm_Update.jsp");
				view.forward(request, response);
				
				break;
				
			case 4:
				
				System.out.println("*****zzzz*****");
				
				
				out.println( "<script>" );
				out.println( "alert(\"修正が完了しました！\");" );
				out.println( "location.href=\"MemberLogAction.do?action_code=sessioncheck&Mem_Nick="+request.getParameter("Mem_Nick")+"&result="+request.getParameter("result")+"\";" );
				out.println( "</script>" );
				break;	
			
			case 5:
				request.setAttribute("upcheck","5");
				request.setAttribute("check","/Main/FirstMain/Fm_Select_Home.jsp");
				view = request.getRequestDispatcher("/Main/FirstMain/Fm_Update.jsp");
				view.forward(request, response);
			break;
				
			}
				
			}else if(action_code.equals("Repass")){
				String numMode = request.getParameter("num");
				int start = numMode != null ? Integer.parseInt(numMode) : -1;
				
					switch(start){
					case 1:
						request.setAttribute("upcheck","6");
						request.setAttribute("check","/Main/FirstMain/Fm_PassUpdate.jsp");
						view = request.getRequestDispatcher("/Main/FirstMain/Fm_Update.jsp");
						view.forward(request, response);	
						break;
					
					case 2:
						boolean check = false;
						mbean.setMem_Nick(request.getParameter("Mem_Nick"));
						mbean.setMem_Pass(request.getParameter("Mem_Pass"));
						
						System.out.println(request.getParameter("Mem_Nick"));
						System.out.println(request.getParameter("Mem_Pass"));
						
						check=mdao.RePass(mbean);
							if(check == true){
								
								System.out.println("*****비밀번호성공");
								
								mbean.setMem_Pass(request.getParameter("Mem_NewPass"));
								int result = mdao.mCheckLevel(mbean);
								mdao.RePassUpdate(mbean);
								out.println( "<script>" );
								out.println( "alert(\"PASS変更が完了しました！\");" );
								out.println( "location.href=\"MemberLogAction.do?action_code=sessioncheck&Mem_Nick="+request.getParameter("Mem_Nick")+"&result="+result+"\";" );
								out.println( "</script>" );
							
							}else{
								
								System.out.println("*****비밀번호오류");
								out.println( "<script>" );
								out.println( "alert(\"PASSが存在しません.\");" );
								out.println( "location.href=\"javascript:history.go(-1)\";" );
								out.println( "</script>" );
							
							}
					
					}
			
			}
	}

}
